﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Utilities.Encoders;

namespace SheepCommon.API.SheepCommands.SheepTransferes
{
    public class SheepTransferQueryHandler: IRequestHandler<SheepTransferQueryCommand, ResultPage<SheepTransferQueryDto>>
    {
        private readonly IBaseRepository<SheepTransfer>_sheepTransferRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;

        public SheepTransferQueryHandler(IBaseRepository<SheepTransfer> sheepTransferRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository)
        {
            _sheepTransferRepository = sheepTransferRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
        }


        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepTransferQueryDto>> Handle(SheepTransferQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepTransferQueryDto>() { Code=(int)CommonErrorCode.Search,TotalCount=0,PagedCount=0};
            try
            {
                var sheepTransferList = await(from a in await _sheepTransferRepository.GetAll()
                                              join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                              join c in await _fieldRepository.GetAll() on a.FieldId equals c.FieldId
                                              select new SheepTransferQueryDto()
                                              {
                                                  SheepTransferId = a.SheepTransferId,
                                                  EarBugles = a.EarBugles,
                                                  CottageId = a.CottageId,
                                                  FieldId = a.FieldId,
                                                  Variety = a.Variety,
                                                  Sex = a.Sex,
                                                  RollOutBuildings = a.RollOutBuildings,
                                                  RollOutColumns = a.RollOutColumns,
                                                  SheepTransferTime = a.SheepTransferTime,
                                                  CreatedOwnerId = a.CreatedOwnerId,
                                                  CreatedTime = a.CreatedTime,
                                              }).ToListAsync();
                if(request.SheepTransferTime != null)
                {
                    sheepTransferList= sheepTransferList.Where(c=>c.SheepTransferTime==request.SheepTransferTime).ToList();
                }
                if (!string.IsNullOrEmpty(request.EarBugles))
                {
                    sheepTransferList = sheepTransferList.Where(c => c.EarBugles == request.EarBugles).ToList();
                }
                if (!string.IsNullOrEmpty(request.RollOutBuildings))
                {
                    sheepTransferList = sheepTransferList.Where(c => c.RollOutBuildings == request.RollOutBuildings).ToList();
                }
                if (!string.IsNullOrEmpty(request.RollOutColumns))
                {
                    sheepTransferList = sheepTransferList.Where(c => c.RollOutColumns == request.RollOutColumns).ToList();
                }
                if (request.CottageId>0)
                {
                    sheepTransferList = sheepTransferList.Where(c => c.CottageId == request.CottageId).ToList();
                }
                if (request.FieldId > 0)
                {
                    sheepTransferList = sheepTransferList.Where(c => c.FieldId == request.FieldId).ToList();
                }
                result.TotalCount = sheepTransferList.Count;
                result.PagedCount =(int)Math.Ceiling((result.TotalCount*1.0)/request.PageSize);
                sheepTransferList = sheepTransferList.OrderBy(c=>c.SheepTransferId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheepTransferList;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }

    }
}
